package site;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.SystemException;

/**
 * Servlet implementation class EnregistrementMembre
 */
public class EnregistrementMembre extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@EJB 
	ConnexionManager cm ;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EnregistrementMembre() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("suivant");
		if (op.equals("suivant")) {
			String choix = request.getParameter("rep") ;
			if (choix.equals("oui")) {
				getServletContext().setAttribute("nouveau",true);
				request.getRequestDispatcher("Connexion.jsp").forward(request,response);
			}
			else {
				getServletContext().setAttribute("nouveau",false);
				request.getRequestDispatcher("formMembre.html").forward(request, response) ;
			}
		}
		else if (op.equals("Next")) {
			String login = (String) request.getSession().getAttribute("login");
			int i = cm.getIdLogin(login).get(0) ;
			String n = request.getParameter("nationalite") ;
			String p = request.getParameter("pays") ;
			String e = request.getParameter("ecole");
			String cardNum = request.getParameter("cardNumber") ;
			try {
			int num = Integer.parseInt(cardNum) ;
			cm.addMembre(i,n,p,e);
			request.getSession().setAttribute("nom",cm.getNom(login)) ;
			request.getSession().setAttribute("prenom",cm.getPrenom(login)) ;
			request.getSession().setAttribute("connexion", true);
			request.getRequestDispatcher("PageInitiale.jsp").forward(request, response) ;
			} catch(Exception ex) {
				ex.printStackTrace() ;
				request.getRequestDispatcher("ComplementForm.html").forward(request, response) ;
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
